jQuery(function($){
	var cookie = $.cookie("safeoa");
	var person =  null;
	if(cookie==null){	
		$.ajax({
			url:'mfunctionList.action',
			type:'POST',
			dataType:'json',
			beforeSend:function(xmlhttprequest){
			},
			success:function(data){
				var jsonString = "";
				$.each(data.listFunctions,function(k,v){
					if(jsonString!=""){
						jsonString = jsonString + ",";
					}
					jsonString =jsonString + '{"X":"'+(k%2+1)+'","Y":"'+(parseInt(k/2)+1)+'","FID":"'+v.FID+'","FNAME":"'+v.FNAME+'","FLINK":"'+v.FLINK+'","MLINK":"'+v.MLINK+'"}';
				});
				person = $.json.decode('{"person":['+jsonString+']}').person;
				addToTable(person);
				$.cookie("safeoa",'{"person":'+$.json.encode(person)+'}',{expires: 9999});
				$('.deleteModule').click(function(){
					var id = $(this).parents('.modbox').attr('id');
					$.each(person,function(i,j){
						if(id==j.FID){
							person.splice(i,1);
							return false;
						}
					});
					$.cookie("safeoa",'{"person":'+$.json.encode(person)+'}',{expires: 9999});
					$(this).parents('.modbox').remove();
				});

				_table=document.getElementById("t_1");
				window.onload = _IG_initDrag(_table);
			},
			error:function(xlt){
			}
		});
	} else {
		person =  $.json.decode(cookie).person;
		addToTable(person);

		$('.deleteModule').click(function(){
			var id = $(this).parents('.modbox').attr('id');
			$.each(person,function(i,j){
				if(id==j.FID){
					person.splice(i,1);
				}
			});
			$.cookie("safeoa",'{"person":'+$.json.encode(person)+'}',{expires: 9999});
			$(this).parents('.modbox').remove();
		});
		_table=document.getElementById("t_1");
		window.onload = _IG_initDrag(_table);
	}
});	


function addToTable(person) {
	$.each(person,function(k,v){
		$('#c_'+v.X).append('<div id='+v.FID+' class="modbox ui-widget ui-widget-content ui-corner-all" style="position:relative">'+
				'<div class=ui-jqgrid-title>'+
				'<table class="mhdr ui-state-default ui-widget-header ui-corner-tl ui-corner-tr ui-helper-clearfix" cellspacing=0 cellpadding=0>'+
				'	<tr>'+
				'		<td id='+v.FID+'_h width="97%" style="height:25px;"><a class=mttli id='+v.FID+' href="'+v.MLINK+'" ><span style="padding-left:5px;">'+v.FNAME+'</span></a></td>'+
				'		<td id='+v.FID+'_e width="3%"><span style="-moz-user-select: none;cursor:pointer" title="删除" onclick="deleteModule()" unselectable="on" class="ui-icon ui-icon-closethick deleteModule"></span></td>'+
				'	</tr>'+
				'</table>'+
			'</div>'+
			'<div class="hpcontent">加载中...</div>'+
		'</div>');
		if(v.FLINK){
			$.ajax({
				url:v.FLINK,
				type:'POST',
				dataType:'json',
				beforeSend:function(xmlhttprequest){
				},
				success:function(data){
					if(data.hpcontent){
						$('#'+v.FID).find('.hpcontent').html(data.hpcontent);
					} else {
						$('#'+v.FID).find('.hpcontent').html("暂无");
					}
				},
				error:function(xlt){
					$('#'+v.FID).find('.hpcontent').html("暂无");
				}
			});
		} else {
			$('#'+v.FID).find('.hpcontent').html("暂无");
		}
	});
	$('.c').append('<div style="position:relative;width:1px;height:1px;"></div>');
}

function defaultSet(){
	$.cookie("safeoa",null);
	location.href = "showHomePageJsp.action";
}
